/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

   ExtTypesTests.cpp. Created on: 01.03.2010 15:49:36 
*/

#include "cpputils/TestUtils.hpp"
#include "cpputils/exttypes/RationalNumberTools.hpp"
#include "cpputils/exttypes/BigInteger.hpp"
#include "cpputils/exttypes/BigDecimal.hpp"

using namespace cpputils::utils;
using namespace cpputils::exttypes;

BOOST_AUTO_TEST_CASE ( TestRationalNumberTools ) {

	boost::rational<boost::int64_t> a(157036,1000);
	boost::rational<boost::int64_t> b(321064,1000);
	boost::rational<boost::int64_t> c = a + b;

	std::string sa = "157.036";
	std::string sb = "321.064";
	std::string s0 = "-100.1000000000";

	boost::rational<boost::int64_t> ua;
	RationalNumberTools::stringToRational(sa, ua);
	boost::rational<boost::int64_t> ub;
	RationalNumberTools::stringToRational(sb, ub);
	boost::rational<boost::int64_t> uc = ua + ub;

	boost::rational<boost::int64_t> us0;
	RationalNumberTools::stringToRational(s0, us0);
	// std::cout << us0 << std::endl;

	// std::cout << std::endl << c << "==" << uc << std::endl;
	BOOST_REQUIRE ( c == uc );

	sa = "157.036e+10";
	boost::rational<boost::int64_t> ud;
	RationalNumberTools::stringToRational(sa, ud);
	boost::rational<boost::int64_t> add;
	RationalNumberTools::stringToRational("0.5", add);
	ud+=add;
	// std::cout << ud << "==" << (boost::format("%.3f") %(((double)ud.numerator())/ud.denominator())).str() << std::endl;

	std::string sOutput;
	RationalNumberTools::rationalToString(add, sOutput);
	BOOST_REQUIRE( sOutput == "0.5" );

	const boost::int64_t MIN_NUMERATOR = 0LL;
	const boost::int64_t MAX_NUMERATOR = 100000LL;
	for(boost::int64_t nNumerator=MIN_NUMERATOR; nNumerator < MAX_NUMERATOR; nNumerator++ ) {
			std::string sDenominator = boost::lexical_cast<std::string>(nNumerator);

			for(unsigned int nIdx = sDenominator.size(); nIdx > 0; nIdx -- ) {
				if( (sDenominator[nIdx-1] == '0')) {
					boost::algorithm::erase_tail(sDenominator, 1);
				}
				else
					break;
			}

			std::string sTestValue = boost::lexical_cast<std::string>(nNumerator)+(sDenominator.empty() ? "": ".")+sDenominator;

			boost::rational<boost::int64_t> nRational;
			RationalNumberTools::stringToRational ( sTestValue, nRational );
			std::string sOutValue;
			RationalNumberTools::rationalToString ( nRational, sOutValue );

			//if((nNumerator % 100000)==0)
				//std::cout << "Testing: " << sTestValue << " Output: " << sOutValue << std::endl;
			BOOST_REQUIRE ( sTestValue == sOutValue );
	}

}

BOOST_AUTO_TEST_CASE ( TestBigInt ) {
	BigInteger a(10L), b(20L), c(20L), d(40L);
	BOOST_REQUIRE ( a < b );
	BOOST_REQUIRE ( a != b );
	BOOST_REQUIRE ( b == c );
	BOOST_REQUIRE ( b > a );
	BOOST_REQUIRE ( a < 20L );
	BOOST_REQUIRE ( a < 20.0 );
	BOOST_REQUIRE ( a > 5L );
	BOOST_REQUIRE ( a > 5.0 );
	BOOST_REQUIRE ( a != 5L );
	BOOST_REQUIRE ( a == 10L );
	BOOST_REQUIRE ( (b+c+a) == (d + BigInteger(10L)) );
	BOOST_REQUIRE ( (b+c+a) == 50L );

	BOOST_REQUIRE ( (b*c/d) == a );

	for(unsigned long i=2L; i < 1000L; i++) {
		BigInteger m(i), n(i-1);
		BOOST_REQUIRE( (unsigned long)(m%n) == i%(i-1) );
	}
}

BOOST_AUTO_TEST_CASE ( TestBigDecimal ) {
	BigDecimal a(10L), b(20L), c(20L), d(40L);
	BOOST_REQUIRE ( a < b );
	BOOST_REQUIRE ( a != b );
	BOOST_REQUIRE ( b == c );
	BOOST_REQUIRE ( b > a );
	BOOST_REQUIRE ( a < BigDecimal(20L) );
	BOOST_REQUIRE ( a < BigDecimal(20.0) );
	BOOST_REQUIRE ( a > BigDecimal(5L) );
	BOOST_REQUIRE ( a > BigDecimal(5.0) );
	BOOST_REQUIRE ( a != BigDecimal(5L) );
	BOOST_REQUIRE ( a == BigDecimal(10L) );
	BOOST_REQUIRE ( (b+c+a) == (d + BigDecimal(10L)) );
	BOOST_REQUIRE ( (b+c+a) == BigDecimal(50L) );

	BOOST_REQUIRE ( (b*c/d) == a );

	BigDecimal half( 1L, 2L );
	// std::cout << half << " " << half.toRationalFormatString() << std::endl;
	BigDecimal third ( 1L, 3L );
	// std::cout << third << " " << third.toRationalFormatString() << std::endl;

	BOOST_REQUIRE ( (half+half) == BigDecimal::ONE );
	BOOST_REQUIRE ( (half*BigDecimal::TWO+half*BigDecimal::TWO) == (BigDecimal::TWO) );

	BigDecimal s0_3 ("0.3"), s0_7("0.7"), s1000000000_3("1000000000.33"), s3000000000_77("3000000000.77");
	BOOST_REQUIRE ( s0_3 + s0_7 == BigDecimal::ONE );

	BOOST_REQUIRE ( s1000000000_3 + s3000000000_77 == BigDecimal("4000000001.1") );

	BigDecimal s1_5;
	s1_5 = "1.5";
	// std::cout << s1_5 << " " << s1_5.toRationalFormatString() << " " << BigDecimal::ONE_AND_HALF << " " << BigDecimal::ONE_AND_HALF.toRationalFormatString() << std::endl;
	BOOST_REQUIRE ( s1_5 == BigDecimal::ONE_AND_HALF );

	BOOST_REQUIRE ( BigDecimal("1.5") == BigDecimal::ONE_AND_HALF );
	BOOST_REQUIRE ( BigDecimal(BigDecimal::ONE_AND_HALF) == BigDecimal::ONE_AND_HALF );

	const unsigned long MIN_NUMERATOR = 0LL;
	const unsigned long MAX_NUMERATOR = 100000LL;
	for(unsigned long nNumerator=MIN_NUMERATOR; nNumerator < MAX_NUMERATOR; nNumerator++ ) {
			std::string sDenominator = boost::lexical_cast<std::string>(nNumerator);

			for(unsigned int nIdx = sDenominator.size(); nIdx > 0; nIdx -- ) {
				if( (sDenominator[nIdx-1] == '0')) {
					boost::algorithm::erase_tail(sDenominator, 1);
				}
				else
					break;
			}

			std::string sTestValue = boost::lexical_cast<std::string>(nNumerator)+(sDenominator.empty() ? "": ".")+sDenominator;

			BigDecimal nRational ( sTestValue );
			std::string sOutValue = nRational.toDecimalFormatString();
			BOOST_REQUIRE ( sTestValue == sOutValue );
	}

}
